require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'dialog',
    'calendar',
    'datepicker'
], function ($, common, ready, template, Collection, Model, dialog, calendar, datepicker) {

    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {

            this.event();
            this.fetch();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                container: 'data-list',
                template: 'data-item',
                url: _global.url.api + 'cash',
                condition: {
                    limit: 10,
                    offset: 0
                },
                model: {
                    tag_name: 'tr',
                    url: _global.url.api + '',
                    event: {}
                },
                callback: function () {
                    loaded();
                }
            });
            $(".show_order").hide();
            this.collection.fetch();
        },
        event: function () {
            var _this = this;
            //日期
            $('.long_date').on('click', function () {
                WdatePicker({
                    el: this.id,
                    dateFmt: 'yyyy-MM-dd'
                });
            });
            $("#type").on("change",function(e){
                if($(this).val()==2){
                    $(".show_order").show();
                }
                _this.collection.condition.type = $(this).val();
                _this.collection.reload();
            });
            $('#btn-search').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.start_time = $('input[name="begintime"]').val();
                _this.collection.condition.end_time = $('input[name="endtime"]').val();
                _this.collection.reload();
            });
            var click_search = function (fun) {
                _this.collection.condition.start_time =  $('input[name="begintime"]').val();
                _this.collection.condition.end_time = $('input[name="endtime"]').val();
                _this.collection.condition.type = $('#type').val();
                _this.collection.callback = function () {
                    if (fun && fun == 'toExcel') {
                        toExcel();
                        _this.collection.callback = function () {
                        }
                    }
                }
                _this.collection.reload();
            }
            var href;
            var toExcelWindow;
            $(".btn-excel").on("click", function (e) {
                e.preventDefault();
                href = $(this).attr('href');
                toExcelWindow = dialog({
                    content: '请稍等 ,正在导出...',
                    width: 200,
                    modal: true
                }).show();
                click_search('toExcel');
            });
            var toExcel = function () {
                toExcelWindow.close();
                console.log(_this.collection.result._count);
                var limit = 1000;
                var params = '';
                var data = _this.collection.condition;
                var total = _this.collection.result._count;

                if (total < 1) {
                    alert('没有需要导出的订单');
                    return false;
                }

                for (var i in data) {
                    if (i == 'limit' || i == 'offset') continue;
                    params = params + '&' + i + '=' + encodeURIComponent(data[i]);
                }
                href = (href.indexOf('?') === -1) ? href + '?' : href;
                href = href + params + '&limit=' + limit + '&offset=';

                if (total > limit) {
                    var showData = {};
                    showData.pages = Math.ceil(total / limit);
                    showData.total = total;
                    showData.limit = limit;
                    showData.href = href;
                    dialog({
                        title: '导出订单列表Excel文件',
                        content: template('to-excel-tpl', showData),
                        width: 600,
                        heigth: 500,
                        modal: true,
                        ok: function () {
                        },
                        cancel: function () {
                        }
                    }).show();
                } else {
                    window.location.href = href;
                }
            }
        }
    };

    new index();

});